﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region MasterData
        public List<tb_MasterData> GetMasterDataByTypeId(int typeId)
        {

            var query = (from obj in this.tb_MasterDatas
                         where obj.tb_MasterDataType.Id == typeId
                         select obj).ToList();
            return query;
        }

        public tb_MasterData GetMasterDateById(int id) 
        {
            return this.tb_MasterDatas.SingleOrDefault(tb_MasterDatas => tb_MasterDatas.Id == id);
        }

        public void CreateMasterData(tb_MasterData entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_MasterDatas.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyMasterData(tb_MasterData entity) 
        {
            tb_MasterData instance = this.tb_MasterDatas.SingleOrDefault(p => p.Id == entity.Id);

            
            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;
            instance.Name = entity.Name;
            instance.Description = entity.Description;
            instance.TypeId = entity.TypeId;

            this.SubmitChanges();
        }
        public void DeleteMasterData(tb_MasterData entity) 
        {
            tb_MasterData deleteEntity = tb_MasterDatas.SingleOrDefault(p => p.Id == entity.Id);
            tb_MasterDatas.DeleteOnSubmit(deleteEntity);
            this.SubmitChanges(); 
        }

        #endregion 
    }
}
